package com.walk.zworld.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Handler
import android.os.Looper
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.video.base.GSYVideoView
import com.walk.zworld.R
import com.walk.zworld.ui.fragment.video.model.DtDetailModel
import com.walk.zworld.ui.fragment.video.model.YouVideoViewModel
import com.walk.zworld.utils.UiUtils
import com.walk.zworld.view.MyGsyVideo2
import com.base.lib.base.utils.ext.view.showToast
import com.base.lib.base.view.BaseMvvmActivity

class YouDetailVideoActivity : BaseMvvmActivity<YouVideoViewModel>() {

    companion object {
        const val TAG = "NewDetailActivity"

        const val EXTRA_VIDEO_ID = "videoId"

        fun start(context: Context, videoId: String?) {
            val starter = Intent(context, YouDetailVideoActivity::class.java)
            starter.putExtra(EXTRA_VIDEO_ID, videoId)
            context.startActivity(starter)
        }
    }

    private var layoutManager: RecyclerView.LayoutManager? = null
    private val videoAdapter: VideoAdapter by lazy { VideoAdapter() }
    private var mPosition = 0

    override fun onBindViewModel(): Class<YouVideoViewModel>  = YouVideoViewModel::class.java

    override fun onBindLayout(): Int  = R.layout.you_activity_dt_detail_video

    override fun initBundle() {
        mViewModel.videoId = intent.getStringExtra(EXTRA_VIDEO_ID).toString()
    }

    override fun initView() {
        compatStatusBar(false)
        setWindowStatusTransparent(this)
        val viewPager2 = findViewById<ViewPager2>(R.id.viewPager2)
        UiUtils.fixViewPager2Rv(viewPager2)

        viewPager2.adapter = videoAdapter
        val recyclerView = viewPager2.getChildAt(0) as RecyclerView
        layoutManager = recyclerView.layoutManager
        viewPager2.registerOnPageChangeCallback(object : OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                mPosition = position
                super.onPageSelected(position)
                //LogUtils.e("position --" + position + ":" + layoutManager.getChildCount());
                startVideo()
                setDotViewState()
            }
        })
    }

    override fun initData() {
        getFirstVideoData()
    }


    override fun onResume() {
        super.onResume()
        if (layoutManager != null) {
            startVideo()
        }
    }

    override fun onStop() {
        super.onStop()
        GSYVideoManager.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        GSYVideoManager.releaseAllVideos()
        /* if (commentListDialog != null) {
            commentListDialog.onDestroy();
        }*/
    }
    /**
     * 获取第一个视频详情数据
     */
    private fun getFirstVideoData() {
        showTransLoadingView()
        mViewModel.getVideoDetail().observe(this) {
            if (it == null) {
                "该内容不存在".showToast()
                finish()
            }else {
                videoAdapter.addData(it)
                hideTransLoadingView()
                getRandomVideoListData()
            }
        }
    }

    /**
     * 获取随机视频列表数据
     */
    private fun getRandomVideoListData() {
        mViewModel.getRandomVideoList().observe(this){
            if (it != null && it.isNotEmpty()) {
                videoAdapter.addData(it)
            }
        }
    }
    /**
     * 设置3个点是否显示
     */
    private fun setDotViewState() {
        val data: List<DtDetailModel> = videoAdapter.data
        val ivDot = findViewById<View>(R.id.ivDot)
        if (data.size > mPosition) {
            val createUser = data[mPosition].createUser
            //是自己创建的动态则显示3个点操作按钮
            if (createUser?.contains("12") == true) {
                ivDot.visibility = View.VISIBLE
            } else {
                ivDot.visibility = View.GONE
            }
        } else {
            ivDot.visibility = View.GONE
        }
    }

    private fun startVideo() {
        //GSYVideoManager.releaseAllVideos();
        Handler(Looper.getMainLooper()).postDelayed({
            val viewByPosition = layoutManager!!.findViewByPosition(mPosition)
            if (viewByPosition != null) {
                val videoPlayer: MyGsyVideo2 = viewByPosition.findViewById(R.id.videoPlayer)
                videoPlayer.isLooping = true
                if (videoPlayer.currentState == GSYVideoView.CURRENT_STATE_PAUSE) {
                    videoPlayer.onVideoResume()
                } else {
                    videoPlayer.startPlayLogic()
                }
            }
        }, 100)
    }


    /**
     * 视频列表
     */
    class VideoAdapter : BaseQuickAdapter<DtDetailModel, BaseViewHolder>(R.layout.you_dt_detail_video_layout) {
//        private var clickCommentListener: DtVideoContentView.OnClickCommentListener? = null
//        fun setClickCommentListener(clickCommentListener: DtVideoContentView.OnClickCommentListener?) {
//            this.clickCommentListener = clickCommentListener
//        }

        override fun convert(holder: BaseViewHolder, item: DtDetailModel) {
            //LogUtils.e("VideoAdapter convert "+item);
            val videoPlayer = holder.getView<MyGsyVideo2>(R.id.videoPlayer)
            videoPlayer.isLooping = true
            videoPlayer.playPosition = holder.bindingAdapterPosition
            videoPlayer.setUp(item.url, true, "")
            videoPlayer.loadCoverImage(item.url)
//            val contentView: DtVideoContentView = holder.getView(R.id.contentView)
            //点赞、评论、收藏数量和状态
//            contentView.setData(item, false)
//            contentView.setClickCommentListener(clickCommentListener)
//            holder.addOnClickListener(R.id.llBottom)
        }
    }


}